<?php
//模型
class UserCardLogModel extends MY_Model {
	function __construct(){
		parent::__construct();
		$this->table = 'user_card_log';
	}
	//分页列表
	function get_list($arr=array(),$page=1,$size=10){
		$where = " where 1 ";
		if(isset($arr['user_id'])){
			$where .= " AND l.user_id ='{$arr['user_id']}' ";
		}
		//课程id
		if(isset($arr['card_id'])){
			$where .= " AND l.card_id ='{$arr['card_id']}' ";
		}
		
		$sql  = "select l.*, c.card_name  from 
				".$this->db->dbprefix.$this->table." as l left join 
				".$this->db->dbprefix."course_card as c on c.id=l.card_id 
				{$where} ORDER BY addtime DESC 
				LIMIT ".($page-1)*$size .",{$size}";
				
		$query = $this->db->query($sql);
		$res = $query->result_array();
		
		foreach($res as $k=>$v){
			$res[$k]['addtime'] = date("Y-m-d H:i:s",$v['addtime']);
		}
		
		$sql  = "select count(*) as num from ".$this->db->dbprefix.$this->table." as l ".$where;
		$query = $this->db->query($sql);
		$row = $query->row_array();		
		return array('list'=>$res,'count' =>$row['num']);		
	}
	//全部列表
	function get_lists($arr=array()){
		$where = " where 1 ";
		if(isset($arr['user_id'])){
			$where .= " AND l.user_id ='{$arr['user_id']}' ";
		}
		//课程id
		if(isset($arr['card_id'])){
			$where .= " AND l.card_id ='{$arr['card_id']}' ";
		}
		
		$sql  = "select l.*, c.card_name  from 
				".$this->db->dbprefix.$this->table." as l left join 
				".$this->db->dbprefix."course_card as c on c.id=l.card_id 				
				{$where}  ";
				
		$query = $this->db->query($sql);
		$res = $query->result_array();
		
		foreach($res as $k=>$v){
			$res[$k]['addtime'] = date("Y-m-d H:i:s",$v['addtime']);
		}
		
		return $res;		
	}
	//详情
	function get_info($id){		
						
		$sql  = "select l.*, c.card_name  from 
				".$this->db->dbprefix.$this->table." as l left join 
				".$this->db->dbprefix."course_card as c on c.id=l.card_id 					 
				where l.id='{$id}'";
		
		$query = $this->db->query($sql);
		$row = $query->row_array();		
		return $row;
	}
	//唯一条件获取详情
	function unique_get_info($field,$value){
		$sql  = "select * from ".$this->db->dbprefix.$this->table." where {$field}='{$value}' ";
		$query = $this->db->query($sql);
		$row = $query->row_array();		
		return $row;
	}
	
}

